$(function(){
	//发放
	var $btnSend = $('#btnSend');
	$btnSend.click(function(){
		bonusSend();
	});
	
	//使用
	var $btnUse = $('#btnUse');
	$btnUse.click(function(){
		bonusUse();
	});
	
	//保存
	var $btnSave = $('#btnSave');
	$btnSave.click(function(){
		bonusSave();
	});
	
	//保存会员编号
	var $btnUserSave = $('#btnUserSave');
	if($btnUserSave.length > 0){
		$btnUserSave.click(function(){
			bonusUserSave();
		});
	}

	var $btnRandomBonus = $('#btnRandomBonus');
	if($btnRandomBonus.length > 0){
		$btnRandomBonus.click(function(){
			RandomBonus();
		});
	}
	
});

//发放
function bonusSend(){

	var id 			= $('#id').val();
	var send_count	= $('#send_count').val();
	var bonus_pwd		= $('input[name="bonus_pwd"]:checked').val();

	if(bonus_pwd != undefined){
		bonus_pwd = 1;
	}else{
		bonus_pwd = 0;
	}
	
	var params = {
		'id'				:id,
		'send_count'		:send_count,
		'bonus_pwd'			:bonus_pwd
	}

	$.post('/index.php/Admin/Bonus/send_do',params,function(data){
		if(data.success){
			alert(data.message);
		}else{
			alert(data.message);
		}
	},'json');
}

//保存
function bonusSave(){

	var id 				= $('#id').val();
	var bonus_repeat	= $('#bonus_repeat').val();

	var params = {
		'id'				:id,
		'bonus_repeat'		:bonus_repeat
	}

	$.post('/index.php/Admin/Bonus/bonus_save',params,function(data){
		if(data.success){
			alert('设置成功');
		}else{
			alert('设置失败');
		}
	},'json');
}

//保存
function bonusUserSave(){

	var id 		= $('#id').val();
	var user_id	= $('#user_id').val();

	var params = {
		'id'			:id,
		'user_id'		:user_id
	}

	$.post('/index.php/Admin/Bonus/bonus_user_save',params,function(data){
		if(data.success){
			alert('设置成功');
		}else{
			alert('设置失败');
		}
	},'json');
}


//保存
function RandomBonus(){

	var id 		= $('#id').val();
	var user_id	= $('#user_id').val();

	var params = {};

	$.post('/index.php/Admin/Bonus/random_bonus_sn',params,function(data){
		if(data.success){
			alert('生成成功');
			$('#bonus_repeat').val(data.data.bonus);
		}else{
			alert('生成失败');
		}
	},'json');
}


function bonusUse(){

	var id 	= $('#id').val();
		
	var params = {
		'id':id,
	}

	$.post('/index.php/Admin/Bonus/bonus_use',params,function(data){
		if(data.success){
			alert('设置成功');
		}else{
			alert('设置失败');
		}
	},'json');
}